package com.spring.component.utils;

import com.google.common.base.Strings;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;

/**
 * @author liushihua
 * @since 2018年04月11日
 */
public class StringUtil {

  /**
   * 字符串不为空，空字符，空格
   * 
   * @param s
   * @return
   */
  public static boolean isNotBlank(String s) {
    return s != null && !"".equals(s.trim());
  }

  /**
   * 字符创为空
   * 
   * @param s
   * @return
   */
  public static boolean isBlank(String s) {
    return !isNotBlank(s);
  }

  /**
   * 所有字符串都不为空
   * 
   * @param ss
   * @return
   */
  public static boolean isAllNotBlank(String... ss) {
    return ss != null && Stream.of(ss).allMatch(StringUtil::isNotBlank);
  }

  /**
   * 有任一字符串为空
   * 
   * @param ss
   * @return
   */
  public static boolean isAnyBlank(String... ss) {
    return ss == null || Stream.of(ss).anyMatch(StringUtil::isBlank);
  }

  /**
   * 匹配字符串是否为数字
   * 
   * @param num
   * @return
   */
  public static boolean isNumeric(String num) {
    if (isBlank(num)) {
      return false;
    }
    Pattern pattern = Pattern.compile("[0-9]*");
    Matcher matcher = pattern.matcher(num);
    return matcher.matches();
  }

  /**
   * 两个字符串都不为空，且相等
   * 
   * @param s1
   * @param s2
   * @return
   */
  public static boolean equals(String s1, String s2) {
    return isAllNotBlank(s1, s2) && s1.equals(s2);
  }

  /**
   * 去除字符串中的回车符、换行符和空格符
   * 
   * @param s
   * @return
   */
  public static String continuous(String s) {
    return s.replaceAll("[\r\n]", "").replaceAll("\\s+", "");
  }

  /**
   * 字符串倒序
   * 
   * @param s
   * @return
   */
  public static String reverse(String s) {
    char[] cc = s.toCharArray();
    StringBuilder sb = new StringBuilder(cc.length);
    for (int i = s.length() - 1; i >= 0; i--) {
      sb.append(cc[i]);
    }
    return sb.toString();
  }


  public static void main(String[] args) {
    System.out.println(isBlank("  "));
    System.out.println(isAllNotBlank("21313", "13"));
    System.out.println(isAnyBlank("2123", null));
    System.out.println(isNumeric(""));
    System.out.println("sdad".split(","));
    System.out.println("sasda:false".substring(5));
    System.out.println(Strings.padStart("abc", 5, 'a'));
    System.out.println(Strings.repeat("aabbsds", 5));
    System.out.println(continuous("asfdfs fdfa d   dsf      df "));
    System.out.println(reverse("njkhg 12 d  f"));
  }
}
